/*jslint sloppy: true */
/*global require, module, PRODUCTION */
var angular = require('angular');
var LM = angular.module('LM', [
	'ngResource',
	'ngMessages',
	'ngRoute',
	'monospaced.qrcode'
]).config(function ($httpProvider, $routeProvider) {
	$httpProvider.defaults.withCredentials = true;
	$httpProvider.interceptors.push('forbidden');

	function isNotLogined($rootScope, $q) {
		var account = $rootScope.account,
			isAccountResolved = account.$resolved,
			checker = $q.defer();

		if (isAccountResolved) {
			checker[account.username ? 'reject' : 'resolve']();
		} else {
			account.$promise.then(checker.reject, checker.resolve);
		}

		return checker.promise;
	}

	function isLogined($rootScope, $q) {
		var account = $rootScope.account,
			isAccountResolved = account.$resolved,
			checker = $q.defer();

		if (isAccountResolved) {
			checker[account.username ? 'resolve' : 'reject']();
		} else {
			account.$promise.then(checker.resolve, checker.reject);
		}

		return checker.promise;
	}

	$routeProvider.when('/', {
		controller: 'Welcome',
		templateUrl: 'module/welcome/page.html'
	}).when('/docs', {
		controller: 'Docs',
		templateUrl: 'module/docs/docs.html'
	}).when('/login', {
		controller: 'Login',
		templateUrl: 'module/account/login.html',
		resolve: {
			isNotLogined: isNotLogined
		}
	}).when('/register', {
		controller: 'Register',
		templateUrl: 'module/account/register.html',
		resolve: {
			isNotLogined: isNotLogined
		}
	}).when('/help/setupVideo', {
		templateUrl: 'module/docs/video.html'
	}).when('/project', {
		controller: 'ProjectList',
		templateUrl: 'module/project/list.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/add', {
		controller: 'CreateProject',
		templateUrl: 'module/project/create.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId', {
		controller: 'Project',
		templateUrl: 'module/project/detail.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId/case', {
		controller: 'CaseList',
		templateUrl: 'module/case/list.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId/case/create', {
		controller: 'CreateCase',
		templateUrl: 'module/case/create.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId/case/:caseId', {
		controller: 'Case',
		templateUrl: 'module/case/detail.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId/task/public', {
		controller: 'TaskPublic',
		templateUrl: 'module/task/public.html'
	}).when('/project/:projectId/task', {
		controller: 'TaskList',
		templateUrl: 'module/task/list.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId/task/:taskId', {
		controller: 'Task',
		templateUrl: 'module/task/detail.html',
		resolve: {
			isLogined: isLogined
		}
	}).when('/project/:projectId/task/:taskId/result', {
		controller: 'Overview',
		templateUrl: 'module/result/overview.html'
//		resolve: {
//			isLogined: isLogined
//		}
	}).when('/project/:projectId/task/:taskId/result/case/:caseIndex', {
		controller: 'CaseLog',
		templateUrl: 'module/result/caseview.html'
//		resolve: {
//			isLogined: isLogined
//		}
	}).when('/updateProbe', {
		templateUrl: 'module/updateProbe/updateProbe.html'
	}).otherwise('/');
}).run(function (Account, $interval, $location, $rootScope) {
	$rootScope.account = Account.get();
	$rootScope.$on('$routeChangeError', function () {
		$location.url($rootScope.account.username ? '/project' : '/');
	});

	$rootScope.app = {
		APIOrigin: require('./config')
	};
}).factory('forbidden', function ($location, $q, $rootScope) {
	return {
		'responseError': function (rejection) {
			if (rejection.status === 403) {
				$location.url('/');
			}

			return $q.reject(rejection);
		}
	};
});

module.exports = LM;
